//app.js
const { SERVICETEL } = require('config/index.js');
const Towxml = require('/assert/towxml/main.js');  //  wowxml把HTML转换为wxml
const { getUserSession, wxUserInfo, wxUserPhone } = require('/utils/user.js');
App({
  onLaunch: function () {
		let self = this
		// 获取用户信息
		// wx.getSetting({
		// 	success: (res) => {
		// 		if (!res.authSetting['scope.userInfo']){  // 跳转授权页
		// 			wx.reLaunch({
		// 				url: '/pages/auth-page/auth-page',
		// 			})
		// 		} 
		// 	}
		// })
	},
	wxAuth(withCredentials) {  // 获取微信授权信息，withCredentials：是否解密用户信息
		let self = this;
		wx.getUserInfo({  // 身份认证接口写好后，替换为身份认证的api
			withCredentials: withCredentials,
			success: (res) => {
				self.globalData.userInfo = { ...self.globalData.userInfo, ...res.userInfo }
				try {
					let yude_openid = wx.getStorageSync('yude_openid');  // 获取本地保存的yude_openid
					if (yude_openid){
						if (withCredentials) {
							wxUserInfo({
								encryptedData: res.encryptedData,
								iv: res.iv
							}).then((res) => {
								self.globalData.userInfo = { ...self.globalData.userInfo, ...res }
							})
							
						} else {
							getUserSession();
	
						}
					} else {
						getUserSession();
					}
				} catch (err) {
					console.log(err)
				}
			},
			fail: (res) => {  // 用户拒绝授权
				console.log(res)
			}
		})
	},
	wxPhone(params, withCredentials) {  // 获取微信授权信息，withCredentials：是否解密用户信息
		let self = this;
		let yude_openid = wx.getStorageSync('yude_openid');  // 获取本地保存的yude_openid
		if (withCredentials) {
			wxUserPhone({
				encryptedData: params.encryptedData,
				iv: params.iv
			}).then((res) => {
				self.globalData.userInfo = { ...self.globalData.userInfo, ...res }
			})
		} else {
			getUserSession();
			self.globalData.isuser = false;
		}
	},
 
	makePhoneCall: function () {
		wx.makePhoneCall({
			phoneNumber: SERVICETEL, //仅为示例，并非真实的电话号码
			success: function (e) {
				console.log(e)
			},
			fail: function () {
				console.log('无此电话')
			}
		})
	},
	towxml: new Towxml(),  // 添加为全局的对象，这样每个页面都可以用到
	// globalData: {
	// 	userInfo: null
	// },
})